<template>
  <el-dialog v-model="states.visible" :title="states.title">
    <el-form style="width: 80%">
      <el-form-item label="品牌名称" label-width="80px">
        <el-input v-model="states.form.tmName" placeholder="请输入品牌名称"></el-input>
      </el-form-item>
      <el-form-item label="品牌LOGO" label-width="80px">
        <!-- el-upload 属性：
          action: 上传的地址
         -->
        <el-upload
          class="avatar-uploader"
          action="/admin/product/product/fileUpload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="states.form.logoUrl" :src="states.form.logoUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
    </el-form>

    <!-- 具名插槽: footer -->
    <template #footer>
      <el-button type="primary" size="default" @click="cancel">取消</el-button>
      <el-button type="primary" size="default" @click="confirm">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive } from 'vue'
import type { TradeMark } from '@/api/product/trademark/types'

defineEmits(['saveSuccess'])
const initFormData = () => {
  return reactive<TradeMark>({
    tmName: '',
    logoUrl: '',
  })
}

const states = reactive({
  visible: false,
  action: '',
  title: `品牌`,
  form: initFormData(),
})

const openDialog = (action: string, row?: TradeMark) => {
  states.action = action
  states.title = `${action}品牌`
  states.visible = true
  if (row) {
    states.form = { ...row }
  } else {
    states.form = initFormData()
  }
}

const handleAvatarSuccess = () => {}
const beforeAvatarUpload = () => {}

// 对话框底部取消按钮
const cancel = () => {
  // 对话框隐藏
  states.visible = false
}

// 对话框底部确定按钮
const confirm = () => {
  states.visible = false
}

defineExpose({
  openDialog,
})
</script>

<style lang="scss" scoped>
.avatar-uploader .avatar {
  display: block;
  width: 178px;
  height: 178px;
}
</style>

<style>
.avatar-uploader .el-upload {
  position: relative;
  overflow: hidden;
  cursor: pointer;
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  width: 178px;
  height: 178px;
  font-size: 28px;
  color: #8c939d;
  text-align: center;
}
</style>
